"""
@author: 江同学呀
@file: tools.py
@date: 2023/8/22 19:45
@desc:
"""

import hashlib
import itertools
import random
import string
from typing import Dict, List, Tuple


def get_random_string() -> str:
    """
    获取随机字符串
    :return:
    """
    char: str = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(32))
    m = hashlib.md5()
    m.update(char.encode("utf-8"))
    return m.hexdigest()


def merge_dict(d1: Dict, d2: Dict) -> Dict:
    """
    合并多级字典
    :param d1:
    :param d2:
    :return: d1
    """
    for k, v in d2.items():
        if k in d1:
            d1_k = d1[k]
            if isinstance(d1_k, dict) and isinstance(v, dict):
                merge_dict(d1[k], v)
            elif isinstance(d1_k, list) and isinstance(v, list):
                d1[k] += v
            else:
                d1[k] = v
        else:
            d1[k] = v
    return d1


def inspect(obj):
    return (attr for attr in dir(obj) if not attr.startswith("__"))


def enumerate_params_combinations(all_params: Dict) -> List[Tuple[Tuple, ...]]:
    """
    枚举所有的参数组合
    :param all_params:
    :return:
    """
    items: List[Tuple] = list(all_params.items())
    param_combinations: List[Tuple[Tuple, ...]] = []
    for r in range(1, len(items) + 1):
        param_combinations.extend(itertools.combinations(items, r))
    return param_combinations


